//
//  NSNotification+Extension.m
//  TableDemo
//
//  Created by jutuo on 2020/3/24.
//  Copyright © 2020 jutuo. All rights reserved.
//

#import "NSNotificationCenter+Extension.h"
#include <pthread.h>

@implementation NSNotificationCenter (Extension)
- (void)postNotificationOnMainThread:(NSNotification *)notification
{
    if (pthread_main_np()) {
        return [self postNotification:notification];
    }
    [self postNotificationOnMainThread:notification waitUntilDone:NO];
}

- (void)postNotificationOnMainThread:(NSNotification *)notification waitUntilDone:(BOOL)wait
{
     if (pthread_main_np()) return [self postNotification:notification];
}
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object
{
    if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:nil];
     [self postNotificationOnMainThreadWithName:name object:object userInfo:nil waitUntilDone:NO];
}
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo
{
    if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
    [self postNotificationOnMainThreadWithName:name object:object userInfo:userInfo waitUntilDone:NO];
}

- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo waitUntilDone:(BOOL)wait
{
    if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
    NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
    if (name) [info setObject:name forKey:@"name"];
    if (object) [info setObject:object forKey:@"object"];
    if (userInfo) [info setObject:userInfo forKey:@"userInfo"];
    [[self class] performSelectorOnMainThread:@selector(_xy_postNotificationName:) withObject:info waitUntilDone:wait];
    
}
+ (void)_xy_postNotification:(NSNotification *)notification
{
    [[self defaultCenter]postNotification:notification];
}

+ (void)_xy_postNotificationName:(NSDictionary *)info
{
    NSString *name = [info objectForKey:@"name"];
    id object = [info objectForKey:@"object"];
    NSDictionary *userInfo = [info objectForKey:@"userInfo"];
       
    [[self defaultCenter] postNotificationName:name object:object userInfo:userInfo];
}
@end
